/**
 * 2013 2013-5-8
 */
package com.surfilter.dp.dao;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.surfilter.dp.model.ModuleModel;

/**
 * 
 */
public class ModuleDao extends BaseDao {

	public List<ModuleModel> getAllModules() {
		List<ModuleModel> retList = new ArrayList<ModuleModel>();
		PreparedStatement pstmt = this.getPstmt("select * from pl_module");
		try {
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				ModuleModel model = new ModuleModel();
				model.setId(rs.getInt("ID"));
				model.setModuleName(rs.getString("MODULE_NAME"));
				model.setModuleText(rs.getString("MODULE_TEXT"));
				model.setPname(rs.getString("PNAME"));
				model.setRemark(rs.getString("REMARK"));
				model.setModuleType(rs.getInt("MODULE_TYPE"));
				model.setCreateTime(rs.getDate("CREATE_TIME"));
				model.setServiceCls(rs.getString("SERVICE_CLS"));
				model.setServiceName(rs.getString("SERVICE_NAME"));
				model.setSortIndex(rs.getInt("SORT_INDEX"));
				model.setValidFlag(rs.getInt("VALID_FLAG"));
				model.setUpdateTime(rs.getDate("UPDATE_TIME"));
				model.setCodePath(rs.getString("CODE_PATH"));
				model.setTableName(rs.getString("TABLE_NAME"));
				retList.add(model);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		this.closePstmt(pstmt);
		return retList;
	}

	public ModuleModel getModuleByName(String name) {
		ModuleModel model = new ModuleModel();
		PreparedStatement pstmt = this
				.getPstmt("select * from pl_module where MODULE_NAME=?");
		try {
			pstmt.setString(1, name);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				model.setId(rs.getInt("ID"));
				model.setModuleName(rs.getString("MODULE_NAME"));
				model.setModuleText(rs.getString("MODULE_TEXT"));
				model.setPname(rs.getString("PNAME"));
				model.setRemark(rs.getString("REMARK"));
				model.setModuleType(rs.getInt("MODULE_TYPE"));
				model.setCreateTime(rs.getDate("CREATE_TIME"));
				model.setServiceCls(rs.getString("SERVICE_CLS"));
				model.setServiceName(rs.getString("SERVICE_NAME"));
				model.setSortIndex(rs.getInt("SORT_INDEX"));
				model.setValidFlag(rs.getInt("VALID_FLAG"));
				model.setUpdateTime(rs.getDate("UPDATE_TIME"));
				model.setCodePath(rs.getString("CODE_PATH"));
				model.setTableName(rs.getString("TABLE_NAME"));
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		this.closePstmt(pstmt);
		return model;
	}
	

	public void updateModule(ModuleModel model) {
		PreparedStatement pstmt = this
				.getPstmt("update pl_module set MODULE_TEXT=?,REMARK=?,SERVICE_CLS=?,SERVICE_NAME=?,SORT_INDEX=?,UPDATE_TIME=?,CODE_PATH=?,TABLE_NAME=? where MODULE_NAME=?");
		try {
			pstmt.setString(1, model.getModuleText());
			pstmt.setString(2, model.getRemark());
			pstmt.setString(3, model.getServiceCls());
			pstmt.setString(4, model.getServiceName());
			pstmt.setInt(5, model.getSortIndex());
			pstmt.setDate(6, new Date(System.currentTimeMillis()));
			pstmt.setString(7, model.getCodePath());
			pstmt.setString(8, model.getTableName());
			pstmt.setString(9, model.getModuleName());
			pstmt.executeUpdate();
			this.closePstmt(pstmt);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @param module
	 */
	public void deleteModule(ModuleModel module) {
		PreparedStatement pstmt = this
				.getPstmt("delete from pl_module where MODULE_NAME=?");
		try {
			pstmt.setString(1, module.getModuleName());
			pstmt.execute();
			this.closePstmt(pstmt);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	/**
	 * @param model
	 */
	public void addNewModule(ModuleModel model) {
		PreparedStatement pstmt = this.getPstmt("insert into pl_module (MODULE_NAME,MODULE_TEXT,PNAME,"+
	"MODULE_TYPE,CREATE_TIME,SERVICE_NAME,SERVICE_CLS,SORT_INDEX,REMARK,TABLE_NAME,CODE_PATH)" +
				"values (?,?,?,?,?,?,?,?,?,?,?)");
		try {
			pstmt.setString(1, model.getModuleName());
			pstmt.setString(2, model.getModuleText());
			pstmt.setString(3, model.getPname());
			pstmt.setInt(4, model.getModuleType());
			pstmt.setDate(5, new Date(System.currentTimeMillis()));
			pstmt.setString(6, model.getServiceName());
			pstmt.setString(7, model.getServiceCls());
			pstmt.setInt(8, model.getSortIndex());
			pstmt.setString(9, model.getRemark());
			pstmt.setString(10, model.getTableName());
			pstmt.setString(11, model.getCodePath());
			pstmt.execute();
			this.closePstmt(pstmt);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
}
